/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwdablu.soumya.lottiebottomnavsample.util;

import com.wwdablu.soumya.lottiebottomnav.util.LogUtil;
import ohos.agp.text.Font;
import ohos.app.Context;
import ohos.app.Environment;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * FontUtil
 *
 * @since 2021-06-11
 */
public final class FontUtil {
    private static final int BYTE1024 = 1024;
    private static final int NOTONE = -1;

    private FontUtil() {
    }

    /**
     * readFontFile
     *
     * @param context context
     * @return File
     */
    private static File readFontFile(Context context) {
        ResourceManager resManager = context.getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/coffeesugar.ttf");
        Resource resource = null;
        try {
            resource = rawFileEntry.openRawFile();
        } catch (IOException e) {
            LogUtil.error("FontUtil", e.getMessage());
        }
        StringBuffer fileName = new StringBuffer("coffeesugar.ttf");

        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName.toString());
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            int index;
            byte[] bytes = new byte[BYTE1024];
            while ((index = resource.read(bytes)) != NOTONE) {
                outputStream.write(bytes, 0, index);
                outputStream.flush();
            }
        } catch (FileNotFoundException e) {
            LogUtil.error("FileNotFoundException", e.getMessage());
        } catch (IOException e) {
            LogUtil.error("IOException", e.getMessage());
        } finally {
            try {
                if (resource != null) {
                    resource.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                LogUtil.error("IOException", e.getMessage());
            }
        }
        return file;
    }

    /**
     * getReadFont
     *
     * @param context context
     * @return Font
     */
    public static Font getReadFont(Context context) {
        File file = readFontFile(context);
        Font.Builder builder = new Font.Builder(file);
        Font font = builder.build();
        return font;
    }
}
